package com.house.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.house.pojo.House;
import com.house.pojo.User;
import com.house.service.HouseService;
import com.house.service.UserService;



/*
* 用户控制器
* */
@Controller
public class UserController {
	
	
	@Autowired
	private UserService userService;
	@Autowired
	private HouseService houseService;
	
	
	
	
	
	//用户登录的方法
	@RequestMapping("/hello")
	public ModelAndView hello(@RequestParam(name="name")String userName,String password) {
		
		User u = new User();
		u.setUserName(userName);
		u.setPassword(password);
		
		User us = userService.login(u);//调用登录的方法
		ModelAndView mav = new ModelAndView();
		if(us!=null) {
			//说明有此用户，则登录成功
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("h_userno", us.getUserno());
			List<House> hlist = houseService.selectUserHouse(map);
			
			mav.addObject("hlist", hlist);//模型
			mav.addObject("user", us);//模型
			mav.setViewName("guanli");//视图
			
		}else {
			
			//则登录失败
			mav.addObject("mess", "登录失败");//模型
			mav.setViewName("login");//视图
		}
		return mav;
	}

	
	
	
	
	
	
	//用户注册的方法
	@RequestMapping("/userRegister")
	public ModelAndView userRegister(@RequestParam(name="name")String userName,@RequestParam(name="username")String uName,@RequestParam(name="telephone")String uTel,String password) {
		
		ModelAndView mav = new ModelAndView();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userName", userName);
		map.put("uName", uName);
		map.put("uTel", uTel);
		map.put("password", password);
		
		int num = userService.register(map);
		if(num>0) {//说明注册成功
			mav.addObject("mess", "注册成功,可以登录!");
			mav.setViewName("login");	
		}else {
			mav.addObject("mess", "注册失败,请重新注册!");
			mav.setViewName("login");	
		}
		
		return mav;
	}
	
	
	
	
	
	
	
	

}
